//
//  MineApi.m
//  Api
//
//  Created by CPX on 2018/7/23.
//  Copyright © 2018年 CPX. All rights reserved.
//

#import "MineApi.h"

/**
 我的页面接口
 @author yzl
 */
@implementation MineApi

+ (void)registerWith:(NSString *)email pwd:(NSString *)pwd code:(NSString *)code success:(Success)success failure:(Failure)failure{
    NSString *url = [NSString stringWithFormat:@"%@%@",RootURL,RegisterPath];
    [NetworkManager POSTWithURL:url params:@{@"email":email,@"password":pwd,@"check_code":code} success:^(id response) {
        if ([self dealWithResponse:response]) {
            success(response[@"data"]);
        }
        else{
            NSError *error = [NSError errorWithDomain:APPResponseError code:10001 userInfo:response];
            failure(error);
        }
    } failure:^(NSError *error) {
        NSData *data = error.userInfo[@"com.alamofire.serialization.response.error.data"];
        if (!data) {
            NSError *error1 = [NSError errorWithDomain:APPResponseError code:10001 userInfo:@{@"msg":error.userInfo[@"NSLocalizedDescription"]}];
            failure(error1);
            return ;
        }
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
        if ([self dealWithResponse:dic]) {
            success(dic[@"token"]);
        }
        else{
            NSError *error1 = [NSError errorWithDomain:APPResponseError code:10001 userInfo:dic?dic:@{}];
            failure(error1);
        }
    }];
}

+ (void)sendCheckCode:(NSString *)email success:(Success)success failure:(Failure)failure{
    NSString *url = [NSString stringWithFormat:@"%@%@",RootURL,SendCheckCode];
    [NetworkManager POSTWithURL:url params:@{@"email":email} success:^(id response) {
        if ([self dealWithResponse:response]) {
            success(nil);
        }
        else{
            NSError *error = [NSError errorWithDomain:APPResponseError code:10001 userInfo:response];
            failure(error);
        }
    } failure:^(NSError *error) {
        NSData *data = error.userInfo[@"com.alamofire.serialization.response.error.data"];
        if (!data) {
            NSError *error1 = [NSError errorWithDomain:APPResponseError code:10001 userInfo:@{@"msg":error.userInfo[@"NSLocalizedDescription"]}];
            failure(error1);
            return ;
        }
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
        if ([self dealWithResponse:dic]) {
            success(nil);
        }
        else{
            NSError *error1 = [NSError errorWithDomain:APPResponseError code:10001 userInfo:dic?dic:@{}];
            failure(error1);
        }
    }];
}

+ (void)loginWith:(NSString *)email pwd:(NSString *)pwd success:(Success)success failure:(Failure)failure{
    NSString *url = [NSString stringWithFormat:@"%@%@",RootURL,LoginPath];
    [NetworkManager POSTWithURL:url params:@{@"email":email,@"password":pwd} success:^(id response) {
        if ([self dealWithResponse:response]) {
            success(response[@"data"]); // email username token
        }
        else{
            NSError *error = [NSError errorWithDomain:APPResponseError code:10001 userInfo:response];
            failure(error);
        }
    } failure:^(NSError *error) {
        NSData *data = error.userInfo[@"com.alamofire.serialization.response.error.data"];
        if (!data) {
            NSError *error1 = [NSError errorWithDomain:APPResponseError code:10001 userInfo:@{@"msg":error.userInfo[@"NSLocalizedDescription"]}];
            failure(error1);
            return ;
        }
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
        if ([self dealWithResponse:dic]) {
            success(dic[@"token"]);
        }
        else{
            NSError *error1 = [NSError errorWithDomain:APPResponseError code:10001 userInfo:dic?dic:@{}];
            failure(error1);
        }
    }];
}

+ (void)resetPwdWith:(NSString *)email pwd:(NSString *)pwd code:(NSString *)code success:(Success)success failure:(Failure)failure{
    NSString *url = [NSString stringWithFormat:@"%@%@",RootURL,ResetPwdPath];
    [NetworkManager POSTWithURL:url params:@{@"email":email,@"password":pwd,@"check_code":code} success:^(id response) {
        if ([self dealWithResponse:response]) {
            success(response[@"token"]);
        }
        else{
            NSError *error = [NSError errorWithDomain:APPResponseError code:10001 userInfo:response];
            failure(error);
        }
    } failure:^(NSError *error) {
        NSData *data = error.userInfo[@"com.alamofire.serialization.response.error.data"];
        if (!data) {
            NSError *error1 = [NSError errorWithDomain:APPResponseError code:10001 userInfo:@{@"msg":error.userInfo[@"NSLocalizedDescription"]}];
            failure(error1);
            return ;
        }
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
        if ([self dealWithResponse:dic]) {
            success(nil);
        }
        else{
            NSError *error1 = [NSError errorWithDomain:APPResponseError code:10001 userInfo:dic?dic:@{@"msg":error.userInfo[@"NSLocalizedDescription"]}];
            failure(error1);
        }
    }];
}

+ (void)FBCreateUser:(NSString *)fbToken success:(Success)success failure:(Failure)failure{
    NSString *url = [NSString stringWithFormat:@"%@%@",RootURL,FBCreateUserPath];
    [NetworkManager POSTWithURL:url params:@{@"access_token":fbToken} success:^(id response) {
        if ([self dealWithResponse:response]) {
            success(response[@"data"]);
        }
        else{
            NSError *error = [NSError errorWithDomain:APPResponseError code:10001 userInfo:response];
            failure(error);
        }
    } failure:^(NSError *error) {
        NSData *data = error.userInfo[@"com.alamofire.serialization.response.error.data"];
        if (!data) {
            NSError *error1 = [NSError errorWithDomain:APPResponseError code:10001 userInfo:@{@"msg":error.userInfo[@"NSLocalizedDescription"]}];
            failure(error1);
            return ;
        }
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
        if ([self dealWithResponse:dic]) {
            success(nil);
        }
        else{
            NSError *error1 = [NSError errorWithDomain:APPResponseError code:10001 userInfo:dic?dic:@{}];
            failure(error1);
        }
    }];
}

+ (void)updateUsername:(NSString *)username portraint:(UIImage *)portrait success:(Success)success failure:(Failure)failure{
    NSString *url = [NSString stringWithFormat:@"%@%@",RootURL,User];
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    if (username.length) {
        params[@"username"] = username;
    }
    NSMutableDictionary *formParams = [NSMutableDictionary dictionary];
    if (portrait) {
        formParams[@"avatar"] = UIImageJPEGRepresentation(portrait, 1);
    }
    
    [NetworkManagerV3 PUTWithURL:url header:[self header] params:params formParams:formParams success:^(id response) {
        if ([self dealWithResponse:response]) {
            success(response[@"data"]);
        }
        else{
            NSError *error = [NSError errorWithDomain:APPResponseError code:10001 userInfo:response];
            failure(error);
        }
    } failure:^(NSError *error) {
        failure(error);
    }];
}

+ (void)registerToken:(NSString *)token deviceId:(NSString *)deviceId{
    if (!token.length) {
        return;
    }
    NSString *url = [NSString stringWithFormat:@"%@%@",RootURL,RegisterToken];
    [NetworkManagerV3 POSTWithURL:url header:[self header] params:@{@"token":token,@"device_id":deviceId,@"terminal_type":@"apns"} success:^(id response) {
        
    } failure:^(NSError *error) {
        
    }];
}

@end
